package com.example.mrwang.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;


public class DrawerActivity extends Activity {
    private View bottomSheet;
    private RelativeLayout relRoot;
    private RelativeLayout relFoot;
    private BottomSheetBehavior behavior;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        btn = (Button) findViewById(R.id.bottom_sheet_dialog_btn);
        bottomSheet = findViewById(R.id.bottom_sheet);
        relRoot = (RelativeLayout)findViewById(R.id.rel_root);
        relFoot = (RelativeLayout) findViewById(R.id.ts);
        behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setState(BottomSheetBehavior.STATE_HIDDEN);

        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, @BottomSheetBehavior.State int newState) {
                String state = "null";
                switch (newState) {
                    case 1:
                        state = "STATE_DRAGGING";
                        break;
                    case 2:
                        state = "STATE_SETTLING";
                        break;
                    case 3:
                        state = "STATE_EXPANDED";
                        break;
                    case 4:
                        state = "STATE_COLLAPSED";
                        break;
                    case 5:
                        state = "STATE_HIDDEN";
                        break;
                }
                Log.d("MainActivity", "newState:" + state);
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//                Log.d("BottomSheetDemo", "slideOffset:" + slideOffset);
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int state = behavior.getState();

                if (state == BottomSheetBehavior.STATE_EXPANDED) {
                    behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                    relRoot.setAlpha(1f);

//                    WindowManager.LayoutParams lp = getWindow().getAttributes();
//                    lp.alpha = 1f;
//                    getWindow().setAttributes(lp);
//                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

                } else {
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    relRoot.setAlpha(0.5f);
                    relFoot.setAlpha(1f);
//                    WindowManager.LayoutParams lp = getWindow().getAttributes();
//                    lp.alpha = 0.5f;
//
//                    getWindow().setAttributes(lp);
//                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                }


            }
        });

    }
}
